/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.core;

public final class DotNetToJavaStringHelper {
  // ------------------------------------------------------------------------------------
  //	This method replaces the .NET static string method 'IsNullOrEmpty'.
  // ------------------------------------------------------------------------------------
  public static boolean isNullOrEmpty(String string) {
    return string == null || string.equals("");
  }

  // ------------------------------------------------------------------------------------
  //	This method replaces the .NET static string method 'Join' (2 parameter version).
  // ------------------------------------------------------------------------------------
  public static String join(String separator, String[] stringarray) {
    if (stringarray == null) {
      return null;
    } else {
      return join(separator, stringarray, 0, stringarray.length);
    }
  }

  // ------------------------------------------------------------------------------------
  //	This method replaces the .NET static string method 'Join' (4 parameter version).
  // ------------------------------------------------------------------------------------
  public static String join(String separator, String[] stringarray, int startindex, int count) {
    String result = "";

    if (stringarray == null) {
      return null;
    }

    for (int index = startindex;
        index < stringarray.length && index - startindex < count;
        index++) {
      if (separator != null && index > startindex) {
        result += separator;
      }

      if (stringarray[index] != null) {
        result += stringarray[index];
      }
    }

    return result;
  }

  // ------------------------------------------------------------------------------------
  //	This method replaces the .NET static string method 'TrimEnd'.
  // ------------------------------------------------------------------------------------
  public static String trimEnd(String string, Character... charsToTrim) {
    if (string == null || charsToTrim == null) {
      return string;
    }

    int lengthToKeep = string.length();
    for (int index = string.length() - 1; index >= 0; index--) {
      boolean removeChar = false;
      if (charsToTrim.length == 0) {
        if (Character.isWhitespace(string.charAt(index))) {
          lengthToKeep = index;
          removeChar = true;
        }
      } else {
        for (int trimCharIndex = 0; trimCharIndex < charsToTrim.length; trimCharIndex++) {
          if (string.charAt(index) == charsToTrim[trimCharIndex]) {
            lengthToKeep = index;
            removeChar = true;
            break;
          }
        }
      }
      if (!removeChar) {
        break;
      }
    }
    return string.substring(0, lengthToKeep);
  }

  // ------------------------------------------------------------------------------------
  //	This method replaces the .NET static string method 'TrimStart'.
  // ------------------------------------------------------------------------------------
  public static String trimStart(String string, Character... charsToTrim) {
    if (string == null || charsToTrim == null) {
      return string;
    }

    int startingIndex = 0;
    for (int index = 0; index < string.length(); index++) {
      boolean removeChar = false;
      if (charsToTrim.length == 0) {
        if (Character.isWhitespace(string.charAt(index))) {
          startingIndex = index + 1;
          removeChar = true;
        }
      } else {
        for (int trimCharIndex = 0; trimCharIndex < charsToTrim.length; trimCharIndex++) {
          if (string.charAt(index) == charsToTrim[trimCharIndex]) {
            startingIndex = index + 1;
            removeChar = true;
            break;
          }
        }
      }
      if (!removeChar) {
        break;
      }
    }
    return string.substring(startingIndex);
  }

  // ------------------------------------------------------------------------------------
  //	This method replaces the .NET static string method 'Trim' when arguments are used.
  // ------------------------------------------------------------------------------------
  public static String trim(String string, Character... charsToTrim) {
    return trimEnd(trimStart(string, charsToTrim), charsToTrim);
  }

  // ------------------------------------------------------------------------------------
  //	This method is used for string equality comparisons when the option
  //	'Use helper 'stringsEqual' method to handle null strings' is selected
  //	(The Java String 'equals' method can't com.inspur.edp.bef.core.be called on a null instance).
  // ------------------------------------------------------------------------------------
  public static boolean stringsEqual(String s1, String s2) {
    if (s1 == null && s2 == null) {
      return true;
    } else {
      return s1 != null && s1.equals(s2);
    }
  }
}
